'use strict';

app.controller('AppCtrl', ['$scope', '$window', '$rootScope',
    function($scope, $window, $rootScope) {
        $rootScope.getSgObj = function(key) {
            var obj = $window.sessionStorage.getItem(key);
            return JSON.parse(obj);
        }
        $rootScope.setSgObj = function(key, value) {
            return $window.sessionStorage.setItem(key, JSON.stringify(value));
        }
    }
]);

app.controller('RegCtrl', ['$scope', '$window', '$rootScope', 'R',
    function($scope, $window, $rootScope, R) {
        $scope.isshow = 1;

        $scope.refresh = function(){
            $scope.isshow = 0;
            setTimeout(function(){
                $scope.isshow = 1;
                $scope.$apply();
            }, 50);
        };
        
        $scope.seconds = 0;

        $scope.sendSMS = function(){
            if (!$scope.graphCode) {
                $scope.error = '未输入图形验证码';
                return;
            }
            if (!$scope.account) {
                $scope.error = '未输入手机账号';
                return;
            }
            R.sendSmsCode({
                "graphCode": $scope.graphCode,             
                "account": $scope.account,
                "type": 0
            }).then(function(data){
                if (data.rtnCode == '0000000'){
        
                    $scope.seconds = 60;
                    window.regInterval = setInterval(function(){
                        if ($scope.seconds == 0){
                            clearInterval(window.regInterval);
                            $scope.$apply();
                            return;
                        }
                        $scope.seconds--;
                        $scope.$apply();
                    }, 1000);

                    $scope.smsCodeDisabled = true;
                }else{
                    $scope.error = data.msg;
                }
            });
        }

        $scope.submit = function(){
            if (!$scope.smsCode) {
                $scope.error = '未输入手机验证码';
                return;
            }
            if (!$scope.pwd) {
                $scope.error = '未输入密码';
                return;
            }
            if ($scope.pwd != $scope.confirmpwd) {
                $scope.error = '请确认密码';
                return;
            }
            if (!$scope.account) {
                $scope.error = '未输入手机账号';
                return;
            }
            R.commonRegist({account: $scope.account, pwd: md5($scope.pwd), smsCode: $scope.smsCode}).then(function(data){
                if (data.rtnCode == '0000000'){
                    $rootScope.setSgObj('LOGINUSER', data.bizData);
                    window.location.href = 'fill.html';
                }else{
                    $scope.error = data.msg;
                }
            }); 
        } 
    }
]);